home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / Bricks demo ƒ / Bricks.c < prev    next >
C/C++ Source or Header  |  1995-11-10  |  8KB  |  289 lines

  1. //**************************************
  2. //* C translation by Richard Bannister *
  3. //*                                    *
  4. //* Titan Software                     *
  5. //* 76 Stillorgan Wood                 *
  6. //* Stillorgan, Co. Dublin             *
  7. //* Republic of Ireland                *
  8. //*                                    *
  9. //* Author of Zap'T'Balls              *
  10. //**************************************
  11. //...with some modifications by Ingemar
  12.  
  13. // Bricks
  14. //
  15. // Demo program for SAT 2.3, by Ingemar Ragnemalm november 1994
  16. //
  17. // The purpose of this demo is to test and demonstrate the support for what I call "resting" sprites.
  18. // If a sprite neither moves, changes face, overlaps a changing sprite nor changes its place in the
  19. // sprite list, then SAT can avoid drawing it.
  20. //
  21. // For programs where all sprites change in every frame (which is the case in many arcade games)
  22. // then this is not of any interest at all, and will just slow things down. For other programs, for
  23. // example board games, Tetris-style games etc, this technique is very useful.
  24. //
  25. // For the moment (SAT 2.3d3), you get the new system by calling SATRun2 instead of SATRun. The name
  26. // of SATRun2 is likely to change in the future.
  27. //
  28. // When you start Bricks, the sprites will not be initialied in order, so since VPositionSort
  29. // is active, they will sort during the first frames. This slows the program down for the first
  30. // few seconds. If this is a problem, it can be avoided by either turning off sorting or
  31. // creating the sprite in the proper places from the start.
  32. //
  33. // Bug note: When using "fast graphics", the cursor will cause some "mouse droppings". This is best avoided
  34. // by hiding the cursor and repllacing it with a SAT sprite. The "proper" way to avoid the problem is to
  35. // call ShieldCursor, but that will make the cursor flicker a lot.
  36.  
  37. #include "SAT.h"
  38.  
  39. enum{
  40.         kNumBricks = 30
  41. };
  42.  
  43. /* prototypes - really not necessary since all routines are in order */
  44. void InitBricks();
  45. pascal void HandleRestingBrick (SpritePtr me);
  46. pascal void HandleFollowMouse (SpritePtr me);
  47. pascal void SetupBrick (SpritePtr me);
  48. void SynchMenus();
  49. void MenuSelection (long theSelection);
  50.  
  51. static FacePtr brickFace[kNumBricks];
  52. EventRecord myEvent;
  53. WindowPtr whichWindow;
  54. MenuHandle appleMenu, fileMenu;
  55. Boolean gUseStagger, gDone, gUseFast, gAllowBackground;
  56. short i;
  57. SpritePtr sp, found;
  58. long theSelection;
  59. Point where;
  60. short theKey;
  61. short whichPart;
  62. Boolean hasEvent;
  63.  
  64. void InitBricks()
  65. {
  66.     for (i = 0; i < (kNumBricks); i++)
  67.         brickFace[i] = SATGetFace(i + 128);
  68. }
  69.  
  70. pascal void HandleRestingBrick (SpritePtr me)
  71. {
  72. }
  73.  
  74. pascal void HandleFollowMouse (SpritePtr me)
  75. {
  76.     Point mousePos;
  77.     if (Button())
  78.     {
  79.         GetMouse(&mousePos);
  80.         me->position.h = me->position.h + mousePos.h - me->speed.h;
  81.         me->position.v = me->position.v + mousePos.v - me->speed.v;
  82.         me->speed = mousePos;
  83.     }
  84.     else
  85.     {
  86.         me->task = &HandleRestingBrick;
  87.     }
  88. }
  89.  
  90. pascal void SetupBrick (SpritePtr me)
  91. {
  92.         me->task = &HandleRestingBrick;
  93.         me->face = brickFace[me->kind];
  94. }
  95.  
  96. void SynchMenus()
  97. {
  98.         CheckItem(fileMenu, 1, (!gUseStagger));
  99.         CheckItem(fileMenu, 2, gUseStagger);
  100.         CheckItem(fileMenu, 3, gUseFast);
  101.         CheckItem(fileMenu, 4, gAllowBackground);
  102. }
  103.  
  104. void MenuSelection (long theSelection)
  105. {
  106.         Str255 name;
  107.         GrafPtr saveport;
  108.         short theMenu, theItem;
  109.  
  110.         theMenu=HiWord(theSelection);
  111.         theItem=LoWord(theSelection);
  112.  
  113.         switch (theMenu)
  114.         {
  115.                 case 128:
  116.                         if (theItem == 1)
  117.                         {
  118.                                 SATReportStr("\pExperimental SAT demo program. Resting sprites allow good speed with many sprites!");
  119.                         }
  120.                         else
  121.                         {
  122.                                 GetPort(&saveport);
  123.                                 GetMenuItemText(appleMenu, 1, name);    // Get Name
  124.                                 OpenDeskAcc(name); // Run the Desk Accessory
  125.                                 SetPort(saveport);
  126.                         }
  127.                 break;
  128.  
  129.                 case 129:
  130.                         switch (theItem)
  131.                         {
  132.                                 case 1:
  133.                                         gUseStagger = false;
  134.                                 break;
  135.  
  136.                                 case 2:
  137.                                         gUseStagger = true;
  138.                                 break;
  139.  
  140.                                 case 3:
  141.                                         gUseFast = !gUseFast;
  142.                                 break;
  143.  
  144.                                 case 4:
  145.                                         gAllowBackground =!gAllowBackground;
  146.                                 break;
  147.  
  148.                                 case 6:
  149.                                         gDone = true;
  150.                                 break;
  151.                         }
  152.                 break;
  153.         }
  154.         SynchMenus();
  155.         HiliteMenu(0);
  156. }
  157.  
  158. main()
  159. {
  160.     SATInitToolbox();
  161.  
  162.     InitBricks();
  163.  
  164.     SATInit(128, 129, 512, 342);
  165.     SATSetPortScreen();
  166.     gUseStagger = true;
  167.     gAllowBackground = true;
  168.     gUseFast = false;
  169.  
  170.     for (i = 0; i < (kNumBricks); i++)
  171.         sp = SATNewSprite(i, SATRand(gSAT.offSizeH - 48),
  172.                 SATRand(gSAT.offSizeV - 64), &SetupBrick);
  173.     for (i = 0; i < (kNumBricks); i++)
  174.         sp = SATNewSprite(i, SATRand(gSAT.offSizeH - 48),
  175.                 SATRand(gSAT.offSizeV - 64), &SetupBrick);
  176.  
  177.     appleMenu = NewMenu(128, "\p\024");
  178.     AppendMenu(appleMenu, "\pAbout Bricks demo…;(-;");
  179.     AppendResMenu(appleMenu, 'DRVR');
  180.     InsertMenu(appleMenu, 0);       // put apple menu at end of menu bar
  181.  
  182.     fileMenu = NewMenu(129, "\pFile");
  183.     AppendMenu(fileMenu, "\pRunSAT/1;RunSAT2/2;Fast graphics/F;Allow background tasks;(-;Quit/Q");
  184.     InsertMenu(fileMenu, 0);    // put file menu at end of menu bar
  185.     DrawMenuBar();
  186.     SynchMenus();
  187.  
  188.     InitCursor();
  189.  
  190.     do
  191.     {
  192.         // if gUseFast then
  193.         // ShieldCursor(gSAT.bounds, Point(0));
  194.  
  195.         if (gUseStagger)
  196.             SATRun2(gUseFast);
  197.         else
  198.             SATRun(gUseFast);
  199.  
  200.         // if gUseFast then
  201.         // ShowCursor;
  202.  
  203.         if (gAllowBackground)
  204.         {
  205.             SystemTask();
  206.             hasEvent = GetNextEvent(everyEvent, &myEvent);
  207.         }
  208.         else
  209.         {
  210.             hasEvent = GetOSEvent(everyEvent, &myEvent);
  211. // or perhaps mDownMask + updateMask + keyDownMask
  212.         }
  213.         if (hasEvent)
  214.         {
  215.             switch (myEvent.what)
  216.             {
  217.                 case updateEvt:
  218.                     if ((WindowPtr)myEvent.message == gSAT.wind.port)
  219.                     {
  220.                         BeginUpdate(gSAT.wind.port);
  221.                         SATRedraw();
  222.                         EndUpdate(gSAT.wind.port);
  223.                     }
  224.                 break;
  225.  
  226.                 case keyDown:
  227.                     {
  228.                         theKey = myEvent.message &charCodeMask;
  229.                         if ((myEvent.modifiers &cmdKey) != 0)
  230.                         {
  231.                                                     MenuSelection(MenuKey(theKey));
  232.                         }
  233.                         // else
  234.                         // DoKey(theKey, theEvent.modifiers)
  235.                     }
  236.                 break;
  237.  
  238.                 case mouseDown:
  239.                     {
  240.                         whichPart = FindWindow(myEvent.where, &whichWindow);
  241.                         switch (whichPart)
  242.                         {
  243.                             case inMenuBar:
  244.                             {
  245.                             theSelection = MenuSelect(myEvent.where);
  246.                                                         MenuSelection(theSelection);
  247.                             }
  248.                             break;
  249.  
  250.                             case inSysWindow:
  251.                                 SystemClick(&myEvent, whichWindow);
  252.                             break;
  253.  
  254.                             case inContent:
  255.                             {
  256.                                 found = nil;
  257.                                 sp = gSAT.sRoot;
  258.  
  259.                                 GetMouse(&where);
  260.  
  261.                                 do
  262.                                 {
  263.                                     if (PtInRect(where, &sp->r)) found = sp;
  264.                                     sp = sp->next;
  265.                                 } while (sp != nil);
  266.  
  267.                                 if (found != nil)
  268.                                 {
  269.                                     if (!(myEvent.modifiers == 0))
  270.                                     {
  271.                                         found->task = nil;
  272.                                     }
  273.                                     else
  274.                                     {
  275.                                         found->task = &HandleFollowMouse;
  276.                                         found->speed = where; // not speed, but storage for old mouse position
  277.                                     }
  278.                                 }
  279.                             }
  280.                             break;
  281.                         }
  282.                     }
  283.                 }
  284.             }
  285.         } while (!(gDone));
  286.  
  287.         SATSoundShutup();
  288. }
  289.